package com.example.demo.demo.rb_tree;

/**
 * 红黑树节点
 * @author: htc
 * @date: Created in 14:10 2018/11/8.
 */
public class RBTreeNode<T extends Comparable<T>> {
    /**
     * 节点值
     */
    private T value;
    /**
     * 左孩子节点
     */
    private RBTreeNode<T> left;

    /**
     * 右孩子节点
     */
    private RBTreeNode<T> right;
    /**
     * 父亲节点
     */
    private RBTreeNode<T> parent;
    /**
     * 节点颜色
     */
    private boolean red;

    /*******    constructor begin   *****/

    public RBTreeNode() {
    }

    public RBTreeNode(T value) {
        this.value = value;
    }

    public RBTreeNode(T value, boolean red) {
        this.value = value;
        this.red = red;
    }

    /*******    constructor end   *****/

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public RBTreeNode<T> getLeft() {
        return left;
    }

    public void setLeft(RBTreeNode<T> left) {
        this.left = left;
    }

    public RBTreeNode<T> getRight() {
        return right;
    }

    public void setRight(RBTreeNode<T> right) {
        this.right = right;
    }

    public RBTreeNode<T> getParent() {
        return parent;
    }

    public void setParent(RBTreeNode<T> parent) {
        this.parent = parent;
    }

    public boolean isRed() {
        return red;
    }

    public void setRed(boolean red) {
        this.red = red;
    }

    public boolean isBlack() {
        return !red;
    }

    public boolean isLeaf() {
        return left == null && right == null;
    }

    public void makeRed() {
        red = true;
    }

    public void makeBlack() {
        red = false;
    }

    @Override
    public String toString() {
        return value.toString();
    }
}
